<?php
	require_once('Class/connexionDB.Class.php');

	$target_dir 	= "text/";
	$target_file 	= $target_dir . basename($_FILES["fileToUpload"]["name"]);
	$uploadOk 		= 1;
	$imageFileType 	= pathinfo($target_file,PATHINFO_EXTENSION);
	$return 		= '';
	$db 			= new connexionDB();
	$exist 			= 0;

	if(isset($_POST["submit"]))
	{
	    $check = strpos($_FILES["fileToUpload"]["name"], '.xml');

	   	// Check if file already exists
		if (file_exists($target_file))
		{
		    $exist = 1;
		}

		if ($_FILES["fileToUpload"]["size"] > 500000)
		{
		    $return = "Sorry, your file is too large.";
		    $uploadOk = 0;
		}
		else if($imageFileType != "xml")
		{
		    $return = "Sorry, only XML files are allowed.";
		    $uploadOk = 0;
		}
	    else if ($uploadOk == 0) 
		{
		    $return = "Sorry, your file was not uploaded.";
		}
		else 
		{
		    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
		    {
		    	if ($exist == 1)
		    	{
		    		unlink($target_dir . $target_file);
		    	}
		        $return = "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
		        $db->uploadXmlFile($_POST['countryName'], basename($_FILES["fileToUpload"]["name"]), $exist);
		    }
		    else
		    {
		        $return = "Sorry, there was an error uploading your file.";
		    }
		}

		header("Location: upload_translate.php?return=".$return ."&upload=".$uploadOk);
		exit;
	}
?>